package com.example.demo.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.demo.R;
import com.example.demo.entity.Joke;

import java.util.List;


public class JokeAdapter extends RecyclerView.Adapter<JokeAdapter.JokeViewHolder> {
    private List<String> jokes;

    public void setData(List<String> jokes){
        this.jokes = jokes;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public JokeViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_joke, parent, false);
        return new JokeViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull JokeViewHolder holder, int position) {
        holder.tvJoke.setText(jokes.get(position));
    }

    @Override
    public int getItemCount() {
        return jokes == null ? 0 : jokes.size();
    }

    class JokeViewHolder extends RecyclerView.ViewHolder{
        TextView tvJoke;

        public JokeViewHolder(@NonNull View itemView) {
            super(itemView);

            tvJoke = itemView.findViewById(R.id.tv_joke);
        }
    }
}
